package utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Vibrator;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.EdgeEffect;
import android.widget.ListView;

import androidx.core.app.ActivityCompat;

import com.ecdc.soexample.App;
//import com.ecdc.touchelx.R;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Create by: chenwei.li
 * Date: 2017/12/9
 * Time: 下午6:51
 * Email: lichenwei.me@foxmail.com
 */

public class OtherUtil {
    private static Vibrator mVibrator;

    public static void setVibrator() {
        mVibrator = (Vibrator) App.context.getSystemService(Service.VIBRATOR_SERVICE);
        mVibrator.vibrate(new long[]{0, 5, 0, 5}, -1);
    }

    /**
     * 根据当前时间获取一个星期的具体天数
     */
    public static List<String> getAWeekDays(String startTime2) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date currentTime_2 = formatter.parse(startTime2);
        List<String> daysList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(currentTime_2);
            calendar2.add(Calendar.DATE, i);
            Date d2 = calendar2.getTime();
            String day2 = sdf2.format(d2);
            daysList.add(day2);
        }
//        getMonthByCurrentTime(startTime,currentFlag);
        return daysList;
//        return  sdf.format(calendar.getTime())+"~"+sdf.format(calendar.getTime());

    }


    /**
     * 获取本周的周一到周日
     */
    private static String getMondayToWeekdayByCurrentWeek(Date startTime) {
        //通过调整日历，获得本天所属周的周一和周日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = new GregorianCalendar();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(startTime);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        String startTime1 = sdf.format(cal.getTime());
        cal.set(Calendar.DATE, cal.get(cal.DATE) + 6);
        String endTime1 = sdf.format(cal.getTime());

        System.out.println("过去七天区间：" + startTime1 + "~" + endTime1);

        return startTime1 + "~" + endTime1;
    }


    /**
     * 根据本周的时间获得上周的周一和周日
     */
    public static String getMondayToWeekdayByLastWeek(Date startTime, int currentFlag) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.DATE, -7 * currentFlag);
        Date d = calendar.getTime();
        String day = sdf.format(d);
//        getMonthByCurrentTime(startTime,currentFlag);
        return getMondayToWeekdayByCurrentWeek(d);
//        return  sdf.format(calendar.getTime())+"~"+sdf.format(calendar.getTime());

    }

    //根据当前时间获取当前的年份
    public static String getYear(Date startTime, int currentFlag) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        calendar.add(Calendar.YEAR, -currentFlag);
        Date d = calendar.getTime();
        String day = sdf.format(d);
        System.out.println("年：" + day);
        return day;
    }

    //根据当前时间获取当前的月份
    public static String getMonthByCurrentTime(Date startTime, int currentFlag) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar a = Calendar.getInstance();
        a.add(Calendar.MONTH, -1 * currentFlag);

        a.set(Calendar.DATE, 1);//把日期设置为当月第一天

        String firstDay = sdf.format(a.getTime());

        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天

        String endDay = sdf.format(a.getTime());

        int maxDate = a.get(Calendar.DATE);
        return firstDay + "～" + endDay;

    }

    //根据当前时间获取当前的月份的天数
    public static int getMonthDayCounts(Date startTime, int currentFlag) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar a = Calendar.getInstance();
        a.add(Calendar.MONTH, -1 * currentFlag);

        a.set(Calendar.DATE, 1);//把日期设置为当月第一天

        String firstDay = sdf.format(a.getTime());

        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天

        String endDay = sdf.format(a.getTime());

        int maxDate = a.get(Calendar.DATE);
        return maxDate;

    }

    /**
     * 根据当前时间获取一个月的具体天数
     */
    public static List<String> getAMonthDays(String startTime2) throws ParseException {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = formatter.parse(startTime2);

        //重置时间为上个月的第一天
        cal.setTime(curDate);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.MONTH, -1);
        cal.setTime(curDate);
        int maxDate = cal.getActualMaximum(Calendar.DATE);
        if (!true) {
            maxDate = cal.get(Calendar.DAY_OF_MONTH);
        }
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < maxDate; i++, cal.add(Calendar.DATE, 1)) {
            Date d = cal.getTime();
            String date = formatter.format(d);
            list.add(date);
        }

        return list;
    }

    /**
     * 根据当前时间获取一年种每个月的开始时间和结束时间,格式(2019-01-01~2019-01-31)
     */
    public static List<String> getAYearEveryMonthFirsttimeToEndtime(String startTime2) throws ParseException {
        int yearInt = Integer.parseInt(startTime2.split("-")[0]);
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, yearInt);
        List<String> day = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            String yearString = yearInt + "";
            String monthString = i + 1 + "";
            c.set(Calendar.MONTH, i);
            int n = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (monthString.length() < 2) {
                day.add(yearString + "-" + "0" + monthString + "-" + "01"+
                        "~"+
                        yearString + "-" + "0" + monthString + "-" + n);

            } else {
                day.add(yearString + "-"  + monthString + "-" + "01"+
                        "~"+
                        yearString + "-" + "0" + monthString + "-" + n);
            }

        }



        return day;
    }

    /**
     * 通过时间获取今天是星期几
     */
    public static int getCurrentXingQiJiByDate(String ddd){
        Calendar c = Calendar.getInstance();
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date dateString = null;
        try {
            dateString = format1.parse(ddd);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        c.setTime(dateString);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        return weekday;
    }

    /**
     * 设置litview拖动到边缘是的颜色
     * @param cxt
     * @param list
     * @param color
     * @throws Exception
     * @throws IllegalAccessException
     */
    @SuppressWarnings("deprecation")
//    public static void setEdge(Context cxt, View list, int color) throws Exception, IllegalAccessException {
//        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//
//            EdgeEffect edgeEffectTop = new EdgeEffect(cxt);
//            edgeEffectTop.setColor(color);
//            EdgeEffect edgeEffectBottom = new EdgeEffect(cxt);
//            edgeEffectBottom.setColor(color);
//
//            try {
//                Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
//                f1.setAccessible(true);
//                f1.set(list, edgeEffectTop);
//
//                Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
//                f2.setAccessible(true);
//                f2.set(list, edgeEffectBottom);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//        }else{
//            int glowDrawableId = cxt.getResources().getIdentifier("overscroll_glow", "drawable", "android");
//            Drawable androidGlow = cxt.getResources().getDrawable(glowDrawableId);
//            assert androidGlow != null;
//            androidGlow.setColorFilter(cxt.getResources().getColor(color), PorterDuff.Mode.SRC_ATOP);
//        }
//    }

    public static void setEdge(Context cxt, View list, int color) throws Exception, IllegalAccessException {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            EdgeEffect edgeEffectTop = new EdgeEffect(cxt);
            edgeEffectTop.setColor(color);
            EdgeEffect edgeEffectBottom = new EdgeEffect(cxt);
            edgeEffectBottom.setColor(color);

            try {
                Field f1 = list.getClass().getDeclaredField("mLeftEdge");
                f1.setAccessible(true);
                f1.set(list, edgeEffectTop);

                Field f2 = list.getClass().getDeclaredField("mRightEdge");
                f2.setAccessible(true);
                f2.set(list, edgeEffectBottom);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
                f1.setAccessible(true);
                f1.set(list, edgeEffectTop);

                Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
                f2.setAccessible(true);
                f2.set(list, edgeEffectBottom);
            } catch (Exception e) {
                e.printStackTrace();
            }


        }else{
            int glowDrawableId = cxt.getResources().getIdentifier("overscroll_glow", "drawable", "android");
            Drawable androidGlow = cxt.getResources().getDrawable(glowDrawableId);
            assert androidGlow != null;
            androidGlow.setColorFilter(cxt.getResources().getColor(color), PorterDuff.Mode.SRC_ATOP);
        }
    }

    /**
     * 获取设备的id
     * @param context
     * @return
     */
    public final static int REQUEST_READ_PHONE_STATE = 1;
    @SuppressLint("HardwareIds")
    public static String getDeviceId(Context context) {
        String deviceId = "";
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (null != tm) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
            } else {
                if (tm.getDeviceId() != null) {
                    deviceId = tm.getDeviceId();
                } else {
                    deviceId = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
                }
            }
            Log.d("deviceId--->", deviceId);
        }
        return "";
    }




}
